package com.stedu.mall.adminservice.service;

import cn.hutool.crypto.SecureUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.stedu.mall.adminservice.mapper.AdminMapper;
import com.stedu.mall.common.bean.Admin;
import com.stedu.mall.common.exception.SteduException;
import com.stedu.mall.common.service.AdminService;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;

@DubboService
public class AdminServiceImpl implements AdminService {
    @Autowired
    private AdminMapper adminMapper;
    @Override
    public Admin login(String username, String password) throws SteduException {
        //根据用户名查询
        Admin admin = adminMapper.selectByUsername(username);
        if (admin == null) {
            throw new SteduException("用户名错误");
        }

        if (admin.getStatus() == 0){
            throw new SteduException("你已被禁用");
        }

        //对用户输入的密码进行加密 - 使用MD5 和 盐进行加密
        String md5Pwd = SecureUtil.md5(SecureUtil.md5(password + admin.getSalt()));
        //对加密之后的密码和数据库中的密码进行比较
        if (!md5Pwd.equals(admin.getPassword())) {
            throw new SteduException("密码错误");
        }
        return admin;
    }

    @Override
    public Admin selectById(Integer id) {
        return adminMapper.selectById(id);
    }

    @Override
    public boolean update(Admin admin) {
        return adminMapper.update(admin) == 1;
    }

    @Override
    public boolean chgPwd(String oldPwd, String newPwd, Integer id) throws SteduException {
        //判断原密码是否正确
        //根据id查询用户信息, 获取原密码和盐
        Admin admin = adminMapper.selectById(id);
        String salt = admin.getSalt();

        if (oldPwd.equals(newPwd)) {
            throw new SteduException("原密码和新密码一致");
        }

        //使用用户输入的原密码和盐加密 -> 加密之后用户输入的密码
        String md5InputOldPwd = SecureUtil.md5(SecureUtil.md5(oldPwd + salt));

        //和数据库中的原密码比较
        if (!admin.getPassword().equals(md5InputOldPwd)){
            throw new SteduException("原密码错误");
        }

        //对新密码结合盐进行加密
        String md5NewPwd = SecureUtil.md5(SecureUtil.md5(newPwd + salt));

        //修改密码
        admin = new Admin();
        admin.setId(id);
        admin.setPassword(md5NewPwd);

        return adminMapper.update(admin) == 1;
    }

    @Override
    public PageInfo<Admin> selectByAll(Admin admin, Integer pageNum, Integer pageSize) {
        //设置分页参数
        PageHelper.startPage(pageNum, pageSize);

        //查询
        List<Admin> admins = adminMapper.selectByAll(admin);

        //创建分页信息
        PageInfo<Admin> goodsPageInfo = new PageInfo<>(admins);

        return goodsPageInfo;
    }

    @Override
    public boolean updateS(Integer id) {
        Admin admin = adminMapper.selectById(id);
        if (admin.getStatus() == 1){
            admin.setStatus(0);
            adminMapper.updateS(admin);
            return true;
        } else if (admin.getStatus() == 0){
            admin.setStatus(1);
            adminMapper.updateS(admin);
            return true;
        } else {
            return false;
        }
    }


}
